# @libevent//:event_core
#   All core event and buffer functionality. This library contains all
#   the event_base, evbuffer, bufferevent, and utility functions.
#
# @libevent//:event_extra
#   This library defines protocol-specific functionality that you may
#   or may not want for your application, including HTTP, DNS, and
#   RPC.
#
# @libevent//:event
#   This library exists for historical reasons; it contains the
#   contents of both libevent_core and libevent_extra. You shouldn’t
#   use it; it may go away in a future version of Libevent.
#
# @libevent//:event_pthreads
#   This library adds threading and locking implementations based on
#   the pthreads portable threading library. It is separated from
#   libevent_core so that you don’t need to link against pthreads to
#   use Libevent unless you are actually using Libevent in a
#   multithreaded way.
#
# @libevent//:event_openssl
#   This library provides support for encrypted communications using
#   bufferevents and the OpenSSL library. It is separated from
#   libevent_core so that you don’t need to link against OpenSSL to
#   use Libevent unless you are actually using encrypted connections.

load("@rules_cc//cc:defs.bzl", "cc_library")
load("@rules_license//rules:license.bzl", "license")

package(
    default_visibility = ["//visibility:private"],
)

license(
    name = "license",
    license_kinds = [
        "@rules_license//licenses/spdx:BSD-3-Clause",
        "@rules_license//licenses/spdx:ISC",
        "@rules_license//licenses/spdx:MIT",
    ],
    license_text = "LICENSE",
)

exports_files([
    "LICENSE",
])

cc_library(
    name = "event_core_private_headers",
    hdrs = [
        "bufferevent-internal.h",
        "changelist-internal.h",
        "defer-internal.h",
        "epolltable-internal.h",
        "evbuffer-internal.h",
        "evconfig-private.h",
        "evconfig-private_apple.h",
        "evconfig-private_linux.h",
        "event-internal.h",
        "event2-config_apple.h",
        "event2-config_common.h",
        "event2-config_linux.h",
        "event2-config_msvc.h",
        "evmap-internal.h",
        "evsignal-internal.h",
        "evthread-internal.h",
        "ht-internal.h",
        "iocp-internal.h",
        "ipv6-internal.h",
        "kqueue-internal.h",
        "log-internal.h",
        "minheap-internal.h",
        "mm-internal.h",
        "ratelim-internal.h",
        "strlcpy-internal.h",
        "time-internal.h",
        "util-internal.h",
    ],
    visibility = [":__subpackages__"],
)

EVENT_CORE_PUBLIC_HEADERS = [
    "include/event.h",
    "include/event2/buffer.h",
    "include/event2/buffer_compat.h",
    "include/event2/bufferevent.h",
    "include/event2/bufferevent_compat.h",
    "include/event2/bufferevent_ssl.h",
    "include/event2/bufferevent_struct.h",
    "include/event2/event.h",
    "include/event2/event-config.h",
    "include/event2/event_compat.h",
    "include/event2/event_struct.h",
    "include/event2/keyvalq_struct.h",
    "include/event2/listener.h",
    "include/event2/tag.h",
    "include/event2/tag_compat.h",
    "include/event2/thread.h",
    "include/event2/util.h",
    "include/event2/visibility.h",
    "include/evutil.h",
]

cc_library(
    name = "event_core",
    srcs = [
        "buffer.c",
        "bufferevent.c",
        "bufferevent_filter.c",
        "bufferevent_pair.c",
        "bufferevent_ratelim.c",
        "bufferevent_sock.c",
        "devpoll.c",
        "epoll.c",
        "event.c",
        "event_tagging.c",
        "evmap.c",
        "evport.c",
        "evthread.c",
        "evutil.c",
        "evutil_rand.c",
        "evutil_time.c",
        "kqueue.c",
        "listener.c",
        "log.c",
        "poll.c",
        "select.c",
        "signal.c",
        "strlcpy.c",
    ] + select({
        "@platforms//os:windows": [
            "buffer_iocp.c",
            "bufferevent_async.c",
            "event_iocp.c",
            "evthread_win32.c",
            "win32select.c",
        ],
        "//conditions:default": [],
    }),
    hdrs = EVENT_CORE_PUBLIC_HEADERS,
    includes = ["include"],
    linkopts = select({
        "@platforms//os:windows": [
            "advapi32.lib",
            "crypt32.lib",
            "iphlpapi.lib",
            "ws2_32.lib",
        ],
        "//conditions:default": [],
    }),
    strip_include_prefix = "include",
    textual_hdrs = [
        # arc4random.c is textually included by evutil_rand.c when
        # arc4random() is not available. It is not compiled by itself.
        "arc4random.c",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":event_core_private_headers",
    ] + select({
        "@platforms//os:windows": [
            "//compat",
        ],
        "//conditions:default": [],
    }),
)

cc_library(
    name = "event_extra_private_headers",
    hdrs = [
        "evrpc-internal.h",
        "http-internal.h",
    ],
    visibility = [":__subpackages__"],
)

EVENT_EXTRA_PUBLIC_HEADERS = [
    "include/evdns.h",
    "include/event2/dns.h",
    "include/event2/dns_compat.h",
    "include/event2/dns_struct.h",
    "include/event2/http.h",
    "include/event2/http_compat.h",
    "include/event2/http_struct.h",
    "include/event2/rpc.h",
    "include/event2/rpc_compat.h",
    "include/event2/rpc_struct.h",
    "include/evhttp.h",
    "include/evrpc.h",
]

cc_library(
    name = "event_extra",
    srcs = [
        "evdns.c",
        "evrpc.c",
        "http.c",
    ],
    hdrs = EVENT_EXTRA_PUBLIC_HEADERS,
    includes = ["include"],
    linkopts = select({
        "@platforms//os:windows": [
            "shell32.lib",
        ],
        "//conditions:default": [],
    }),
    strip_include_prefix = "include",
    visibility = ["//visibility:public"],
    deps = [
        ":event_core",
        ":event_core_private_headers",
        ":event_extra_private_headers",
    ] + select({
        "@platforms//os:windows": [
            "//compat",
        ],
        "//conditions:default": [],
    }),
)

cc_library(
    name = "event",
    hdrs = EVENT_CORE_PUBLIC_HEADERS + EVENT_EXTRA_PUBLIC_HEADERS,
    visibility = ["//visibility:public"],
    deps = [
        ":event_core",
        ":event_extra",
    ],
)

cc_library(
    name = "event_openssl_private_headers",
    hdrs = [
        "openssl-compat.h",
    ],
    tags = [
        "requires_openssl",
    ],
    visibility = [":__subpackages__"],
    deps = [
        ":event_core",
        ":event_core_private_headers",
        "@openssl//:crypto",
        "@openssl//:ssl",
    ],
)

cc_library(
    name = "event_openssl",
    srcs = [
        "bufferevent_openssl.c",
    ],
    includes = ["include"],
    tags = [
        "requires_openssl",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":event_core",
        ":event_core_private_headers",
        ":event_openssl_private_headers",
        "@openssl//:crypto",
        "@openssl//:ssl",
    ],
)

cc_library(
    name = "event_pthreads",
    srcs = select({
        "@platforms//os:linux": ["evthread_pthread.c"],
        "@platforms//os:macos": ["evthread_pthread.c"],
        "//conditions:default": [],
    }),
    includes = ["include"],
    linkopts = select({
        "@platforms//os:linux": ["-pthread"],
        "@platforms//os:macos": ["-pthread"],
        "//conditions:default": [],
    }),
    visibility = ["//visibility:public"],
    deps = [
        ":event_core",
        ":event_core_private_headers",
    ],
)
